package com.yanqu.road.server.manager.activity.peakseacraft;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.peakseacraft.config.PeakSeacraftActivityConfig;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.manager.activity.ActivityMgr;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PeakSeacraftActivityGroup {
    private int activityId;
    private Map<Integer, PeakSeacraftActivity> activityMap = new ConcurrentHashMap<>();

    public PeakSeacraftActivityGroup(int activityId) {
        this.activityId = activityId;
    }

    public int getActivityId() {
        return activityId;
    }

    public Map<Integer, PeakSeacraftActivity> getActivityMap() {
        return activityMap;
    }

    public void setActivityMap(Map<Integer, PeakSeacraftActivity> activityMap) {
        this.activityMap = activityMap;
    }

    public PeakSeacraftActivity getActivity(int unionGroup) {
        ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
        if (!ActivityHelper.activityInShowTime(activityInfo)) {
            return null;
        }
        PeakSeacraftActivityConfig config = PeakSeacraftConfigMgr.getConfigMap().get(activityId);
        if (config == null) {
            config = PeakSeacraftConfigMgr.reloadConfig(activityInfo);
        }
        PeakSeacraftActivity activity = activityMap.get(unionGroup);
        if (activity == null) {
            synchronized (activityMap) {
                activity = activityMap.get(unionGroup);
                if (activity == null) {
                    activity = new PeakSeacraftActivity(config, unionGroup);
                    activityMap.put(unionGroup, activity);
                }
            }
        }
        return activity;
    }

    public void save(){
        for (PeakSeacraftActivity activity : activityMap.values()) {
            activity.save();
        }
    }

    /**
     * 锁定晋级联盟 在这里分组
     */
    public synchronized void lockPromotionUnionTask() {
        int curRound = PeakSeacraftMgr.getCurRound(activityId);
        for (PeakSeacraftActivity item : activityMap.values()) {
            // 当前定时器执行到轮次
            synchronized (item) {
                int round = item.getActivityCurRound();
                for (int i = round; i <= curRound; i++) {
                    item.lockPromotionUnion(i);
                }
            }
        }
    }

    /**
     * 第一阶段一开始就 向区服索要 联盟数据 和联盟成员的门客数据
     */
    public synchronized void synUnionInfoTask() {
        for (PeakSeacraftActivity item : activityMap.values()) {
            // 当前定时器执行到轮次
            synchronized (item) {
                item.synUnionInfo();
            }
        }
    }

    /**
     * 抵达任务
     */
    public void arrivalTask() {
        for (PeakSeacraftActivity item : activityMap.values()) {
            synchronized (item) {
                item.arrivalTask();
            }
        }
    }

    /**
     * 更新榜单消息
     */
    public void updateUnionRankMsgTask() {
        for (PeakSeacraftActivity item : activityMap.values()) {
            synchronized (item) {
                item.sortAndParseUnionRankMsg();
            }
        }
    }
}
